<?php
declare(strict_types=1);

namespace WebApp\lib;

use WebApp\lib\util\Util;

/**
 * Class Config
 * @package WebApp\lib
 */
class Config
{
    private static $config = [];

    /**
     * @param $fileName
     * @param $key
     * @return mixed
     * @throws \ErrorException
     */
    public static function getConf($fileName, $key)
    {
        $conf_arr = Util::hasArr(self::$config, $fileName);
        if ($conf_arr) {
            return $conf_arr[$key];
        } else {
            $path = self::getFilePath($fileName);
            if (!is_file($path)) {
                throwError("没有找到该配置文件".$path);
            }
            $conf_arr = require_once($path."");
            self::$config[$fileName] = $conf_arr;
            return $conf_arr[$key];
        }
    }

    /**
     * @param $fileName
     * @return mixed|string
     * @throws \ErrorException
     */
    public static function getConfArr($fileName)
    {
        $conf_arr = Util::hasArr(self::$config, $fileName);
        if ($conf_arr) {
            return $conf_arr;
        } else {
            $path = self::getFilePath($fileName);
            if (!is_file($path)) {
                throwError("没有找到该配置文件".$path);
            }
            $conf_arr = require_once($path."");
            self::$config[$fileName] = $conf_arr;
            return $conf_arr;
        }
    }

    /**
     * @param $fileName
     * @return string
     */
    private static function getFilePath($fileName)
    {
        return ROOT_DIR."src/config/".$fileName.".php";
    }
}
